package com.ym.manager.admin.terminal;

import com.ym.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author: dhf
 * @ClassName: AppController
 * @Date: 2022/8/30 12:22
 * @Description:
 * @Version: 1.0
 */
@Controller
@RequestMapping("/app")
public class AppController {
    @Autowired
    private AppService appService;

    @RequestMapping("/index")
    public String index(){
        return "/app/list";
    }

    @RequestMapping("/getList")
    @ResponseBody
    public String getList(){
        Map<String,Object> result = ResultUtil.result();
        try {
            List<Map<String,Object>> list = appService.getList();
            result.put("data",list);
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }
    @RequestMapping("/update/index")
    public String updateIndex(Model model, long id){
        model.addAttribute("appInfo",appService.appInfo(id));
        return "/app/edit";
    }
    @RequestMapping("/update/edit")
    @ResponseBody
    public String updateEdit(Model model, long id, String version,String app_version,String version_code,int is_forced_updating,String updating_content,String app_url){
        Map<String,Object> result = ResultUtil.result();
        try {
            int count  = appService.updateEdit(id,version,app_version,version_code,is_forced_updating,updating_content,app_url);
            if (count <= 0) {
                result.put("code",600);
                result.put("msg","修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }
}
